package com.studydemo.comm.mybatisconfig.aop;

import com.studydemo.comm.mybatisconfig.DataSourceContextHolder;
import com.studydemo.comm.mybatisconfig.enumutil.DataSourceType;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

/**
 * 在service层决定数据源
 * 
 * 必须在事务AOP之前执行，所以实现Ordered,order的值越小，越先执行
 * 如果一旦开始切换到写库，则之后的读都会走写库
 * 
 * @author leo_lei
 *
 */
@Aspect
@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true)
@Component
public class DataSourceAopInService implements PriorityOrdered {

private static Logger log = LoggerFactory.getLogger(DataSourceAopInService.class);
	@Before(
			"	execution(* com.studydemo.dao.mapper.*.select*(..)) "
			+ " || @annotation(com.studydemo.comm.mybatisconfig.annotion.ReadDataSource) "
			)
    public void setReadDataSourceType() {
		System.out.println("进入切面的读1111");
		//如果已经开启写事务了，那之后的所有读都从写库读
		System.out.println(DataSourceContextHolder.getReadOrWrite());
		if(!DataSourceType.write.getType().equals(DataSourceContextHolder.getReadOrWrite())){
			DataSourceContextHolder.setRead();
//			DataSourceContextHolder.setWrite();
		}

    }

    @Before("execution(* com.studydemo.dao.mapper.*.insert*(..)) "
    		+ " || execution(* com.studydemo.dao.mapper.*.add*(..))"
    		+ " || execution(* com.studydemo.dao.mapper.*.save*(..))"
    		+ " || execution(* com.studydemo.dao.mapper.*.update*(..))"
    		+ " || execution(* com.studydemo.dao.mapper.*.del*(..))"
			+ " || @annotation(com.studydemo.comm.mybatisconfig.annotion.WriteDataSource) ")
    public void setWriteDataSourceType() {
		System.out.println("进入切面的写1111");
        DataSourceContextHolder.setWrite();
    }
    

	/*@Before("execution(* com.leo.separation.service..*.*(..)) "
			+ " and @annotation(com.leo.separation.config.dbConfig.ReadDataSource) ")
	public void setReadDataSourceType() {
		//如果已经开启写事务了，那之后的所有读都从写库读
		if(!DataSourceType.write.getType().equals(DataSourceContextHolder.getReadOrWrite())){
			DataSourceContextHolder.setRead();
		}
	    
	}

	@Before("execution(* com.leo.separation.service..*.*(..)) "
			+ " and @annotation(com.leo.separation.config.dbConfig.WriteDataSource) ")
	public void setWriteDataSourceType() {
	    DataSourceContextHolder.setWrite();
	}*/
    
	@Override
	public int getOrder() {
		/**
		 * 值越小，越优先执行
		 * 要优于事务的执行
		 * 在启动类中加上了@EnableTransactionManagement(order = 10) 
		 */
		return 3;
	}

}
